package com.fenqing.leetcode.code.t0_200.t100_200;


import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

/**
 * @author fenqing
 */
public class Topic150 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic150(String input) {
        super(input);
    }

    static Set<String> OPERATORS = new HashSet<>();

    static {
        OPERATORS.add("+");
        OPERATORS.add("-");
        OPERATORS.add("*");
        OPERATORS.add("/");
    }

    public static int eval(String num1, String num2, String symbol) {
        Integer number1 = Integer.parseInt(num1);
        Integer number2 = Integer.parseInt(num2);
        switch (symbol) {
            case "+":
                return number1 + number2;
            case "-":
                return number2 - number1;
            case "*":
                return number1 * number2;
            case "/":
                return number2 / number1;
            default:break;
        }
        return 0;
    }

    @Execute
    public int evalRPN(String[] tokens) {
        Stack<String> numberStack = new Stack<>();
        for (String token : tokens) {
            int type = type(token);
            switch (type) {
                case 1:
                    String pop1 = numberStack.pop();
                    String pop2 = numberStack.pop();
                    numberStack.push(String.valueOf(eval(pop1, pop2, token)));
                    break;
                case 2:
                    numberStack.push(token);
                    break;
                default:break;
            }
        }
        return Integer.parseInt(numberStack.pop());
    }

    public static int type(String str){
        if (OPERATORS.contains(str)){
            return 1;
        } else {
            return 2;
        }
    }

    public static void main(String[] args) {
        int run = new Topic150("[\"4\",\"13\",\"5\",\"/\",\"+\"]").run();
        System.out.println(run);
    }

}
